//
//  DetailViewController.m
//  ShouYi
//
//  Created by yrapp on 16/2/23.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "DetailViewController.h"
#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#import "WXApi.h"
@interface DetailViewController ()<UIScrollViewDelegate,UMSocialDataDelegate,UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UIView *headerView;

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addWebView];
    
    [self addHeaderAndFootView];
    
    
    
    
    
    [self addUrl:self.webUrl];
    
    // Do any additional setup after loading the view.
}
//添加header footView
- (void)addHeaderAndFootView {
    
    //webView添加退出标题
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 64)];
    self.headerView.backgroundColor = kBackGroundColor;
    [self.view addSubview:self.headerView];
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(20, 20 + 7, 20, 30);
    [backButton setImage:[UIImage imageNamed:@"icon_fh"] forState:(UIControlStateNormal)];
    backButton.imageEdgeInsets = UIEdgeInsetsMake(5, 3, 5, 3);
    
    [backButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:backButton];
    
    
    
    //一键分享
        UIButton *footButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    footButton.backgroundColor = [UIColor colorWithRed:235.0/255 green:235.0/255 blue:235.0/255 alpha:1];
    footButton.backgroundColor = [UIColor clearColor];
    
        footButton.frame = CGRectMake(0, kHeight - 49, kWidth, 49);
        [footButton setImage:[UIImage imageNamed:@"分享赚钱"] forState:(UIControlStateNormal)];
    [footButton addTarget:self action:@selector(shareToWechat) forControlEvents:UIControlEventTouchUpInside];
        footButton.imageEdgeInsets = UIEdgeInsetsMake(2.5, 5, 2.5, 5);
        [self.view addSubview:footButton];

}
// 分享到朋友圈
- (void)shareToWechat{
    // 微信
    [UMSocialWechatHandler setWXAppId:kWXAppID appSecret:kWXAppKey url:self.webUrl];
    [WXApi registerApp:kWXAppID];
    // QQ QQ空间
    [UMSocialQQHandler setQQWithAppId:@"1105084263" appKey:@"7YSou5Du4KPEOgVL" url:self.webUrl];
    
    [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:self.imagesrc];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"507fcab25270157b37000010"
                                      shareText:self.webUrl
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatTimeline,nil]
                                       delegate:self];

}

- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


//添加webView
- (void)addWebView {
    
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -20, kWidth, kHeight + 20)];
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    self.webView.scrollView.delegate = self;
    [self.view addSubview:self.webView];
    
}

//加载内容
- (void)addUrl:(NSString *)url {
    
    NSURL *tempUrl = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:tempUrl];
    [self.webView loadRequest:request];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView.contentOffset.y > 64) {
        
        self.headerView.alpha = 1 - (scrollView.contentOffset.y - 64) / 500.0;
        
    }
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error {
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
